package com.jerei.utils.spring;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

/**
 * Created by sunfeihu on 2016/10/20.
 */
public class PageBean<T> {

    private int nowPage;

    private int pageSize;

    private int totalRows;

    private int totalPages;

    private int startIndex;

    private int lastIndex;

    private List<T> resultList;

    public PageBean(String sql, Object[] params, int nowPage, int pageSize, RowMapper<T> rowMapper, JdbcTemplate jdbcTemplate) {
        if (jdbcTemplate == null) {
            throw new IllegalArgumentException("Page.jTemplate is null");
        } else if (sql == null || sql.equals("")) {
            throw new IllegalArgumentException("Page.sql is empty");
        }
        //设置每页显示记录数
        setPageSize(pageSize);
        //设置要显示的页数
        setNowPage(nowPage);
        //计算总记录数
        StringBuffer totalSQL = new StringBuffer(" SELECT count(*) FROM ( ");
        totalSQL.append(sql);
        totalSQL.append(" ) totalTable ");
        //总记录数
        setTotalRows(jdbcTemplate.queryForObject(totalSQL.toString(), params, Integer.class));
        //计算总页数
        setTotalPages();
        //计算起始行数
        setStartIndex();
        //计算结束行数
        setLastIndex();
        //使用mysql时直接使用limits
        StringBuffer paginationSQL = new StringBuffer();
        paginationSQL.append(sql);
        paginationSQL.append(" limit " + startIndex + "," + lastIndex);
        //装入结果集
        if (null != params && params.length > 0) {
            setResultList(jdbcTemplate.query(paginationSQL.toString(), rowMapper, params));
        } else {
            setResultList(jdbcTemplate.query(paginationSQL.toString(), rowMapper));
        }
    }

    public int getNowPage() {
        return nowPage;
    }

    public void setNowPage(int nowPage) {
        this.nowPage = nowPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public int getTotalPages() {
        return totalPages;
    }

    private void setTotalPages() {
        if ((this.totalRows % this.pageSize) == 0) {
            this.totalPages = totalRows / pageSize;
        } else {
            this.totalPages = (totalRows / pageSize) + 1;
        }
    }

    public int getStartIndex() {
        return startIndex;
    }

    private void setStartIndex() {
        this.startIndex = (nowPage - 1) * pageSize;
    }

    public int getLastIndex() {
        return lastIndex;
    }

    private void setLastIndex() {
        if (totalRows < pageSize) {
            this.lastIndex = totalRows;
        } else if ((totalRows % pageSize == 0) || (totalRows % pageSize != 0 && nowPage < totalPages)) {
            this.lastIndex = nowPage * pageSize;
        } else if (totalRows % pageSize != 0 && nowPage == totalPages) {
            this.lastIndex = totalRows;
        }
    }

    public List<T> getResultList() {
        return resultList;
    }

    public void setResultList(List<T> resultList) {
        this.resultList = resultList;
    }
}
